package com.luo.locationapp.activity;

import org.json.JSONException;
import org.json.JSONObject;

import com.luo.locationapp.R;
import com.luo.locationapp.customview.CustomEditText;
import com.luo.locationapp.interfaces.impl.GeneralProcessorInterfaceImpl;
import com.luo.locationapp.link.LinkServiceTask;
import com.luo.locationapp.utils.CustomDialog;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationSet;
import android.view.animation.AnimationUtils;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

public class RegisterActivity extends Activity implements OnClickListener{
	private TextView title;
	private CustomEditText cetNickName,cetPassword,cetConfirmpw;
	private LinearLayout container;
	private Button btCancel, btConfirm;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		getWindow().requestFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_register);
		
		findView();
		
		init();
		
	}
	
	@Override
	protected void onStart() {
		super.onStart();
		AnimationSet animationSet = new AnimationSet(true);
		animationSet.addAnimation(AnimationUtils.loadAnimation(RegisterActivity.this, R.anim.translatedown));
		animationSet.addAnimation(AnimationUtils.loadAnimation(RegisterActivity.this, R.anim.alpha));
		animationSet.setAnimationListener(new AnimationListener() {
			@Override
			public void onAnimationStart(Animation arg0) {
				container.setVisibility(View.VISIBLE);
			}
			@Override
			public void onAnimationRepeat(Animation arg0) {
				
			}
			@Override
			public void onAnimationEnd(Animation arg0) {
				
			}
		});
		container.startAnimation(animationSet);
	}
	private void findView(){
		title = (TextView) findViewById(R.id.generalbar_tips);
		container = (LinearLayout) findViewById(R.id.register_ll_container);
		btCancel = (Button) findViewById(R.id.register_bt_cancel);
		btConfirm = (Button) findViewById(R.id.register_bt_commit);
		cetNickName = (CustomEditText) findViewById(R.id.register_edt_nickname);
		cetPassword = (CustomEditText) findViewById(R.id.register_edt_password);
		cetConfirmpw = (CustomEditText) findViewById(R.id.register_edt_confirmpassword);
	}
	
	private void init() {
		title.setText(R.string.register);
		
		//listener
		btCancel.setOnClickListener(this);
		btConfirm.setOnClickListener(this);
	}

	@Override
	public void onClick(View arg0) {
		switch(arg0.getId()){
		case R.id.register_bt_cancel:
			finish();
			break;
		case R.id.register_bt_commit:
			final String nickName = cetNickName.getText();
			String password = cetPassword.getText();
			String confirmPW = cetConfirmpw.getText();
			if("".equals(nickName) || "".equals(password)){
				CustomDialog.createCustomMessageDlg(RegisterActivity.this, ("".equals(nickName)?"昵称":"密码")+"不能为空！", new OnClickListener() {
					@Override
					public void onClick(View arg0) {
						if("".equals(nickName)){
							cetNickName.setFocus();
						}else{
							cetPassword.setFocus();
						}
					}
				});
				return;
			}
			if(!password.equals(confirmPW)){
				CustomDialog.createCustomMessageDlg(RegisterActivity.this, "两次密码输入不一致！", null);
				cetPassword.setText("");
				cetConfirmpw.setText("");
				cetPassword.setFocus();
				return;
			}
			try {
				JSONObject registerInfo = new JSONObject();
				registerInfo.put("nickName", nickName);
				registerInfo.put("password", password);
				registerInfo.put("confirmpassword", confirmPW);
				new LinkServiceTask(RegisterActivity.this,new GeneralProcessorInterfaceImpl(RegisterActivity.this)).execute("register", registerInfo.toString());
			} catch (JSONException e) {
				e.printStackTrace();
			}
			break;
		default:
			break;
		}
	}
}
